const loginMethods = {
  /**
   * 刷新token
   */
  refreshToken: async() => {
    const expiresTimeStamp = uni.getStorageSync('expiresTimeStamp')
    // if (!uni.getStorageSync('Token') || !expiresTimeStamp) return
    const isExpires = expiresTimeStamp < new Date().getTime()
    const isRefreshing = uni.getStorageSync('isRefreshingToken')
    const refreshToken=uni.getStorageSync('refreshToken')
    // if (!isExpires || isRefreshing) {
    //   return
    // }
    uni.setStorageSync('isRefreshingToken', true)
    http.request({
      url: '/refresh',
      method: 'GET',
      data:{
        refreshToken:refreshToken
      },
      login: true,
      isRefreshing: true,
      dontTrunLogin: true
    }).then((res) => {
      if(res.code==1){

        uni.setStorageSync('expiresTimeStamp', 1200000+ new Date().getTime())
        // 把token存入缓存，请求接口数据时要用
        uni.setStorageSync('Token', res.map.accessToken)
        uni.setStorageSync("refreshToken", res.map.refreshToken)
      }
      uni.setStorageSync('isRefreshingToken', false)
    }).catch(() => {
      uni.reLaunch({
        url:'/src/pages/index/index'
      })
      uni.removeStorageSync('expiresTimeStamp')
      uni.removeStorageSync('Token')
      uni.removeStorageSync('refreshToken')
      uni.removeStorageSync('hadLogin')
      uni.showToast({
        title:"请重新登录",
        icon:error
      })
    })
  },

  /**
   * 设置登录后的跳转地址
   */
  setRouteUrlAfterLogin: () => {
    const pages = getCurrentPages()
    // 登录后的回跳地址
    if (pages[pages.length - 1].route.indexOf('user-login') === -1) {
      uni.setStorageSync('routeUrlAfterLogin', pages[pages.length - 1].$page.fullPath)
    }
  }
}

export default loginMethods
